package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    @Transactional//事务的标识注解，把方法体里面的业务看成一个整体
    @Override
    public void saveWithFlavors(DishDto dishDto) {
        //保存菜品的基本信息
        save(dishDto);
        //完善口味信息里面的dishId
        dishDto.getFlavors().forEach(dishFlavor ->dishFlavor.setDishId(dishDto.getId()));

        //保存菜品的口味信息
        dishFlavorService.saveBatch(dishDto.getFlavors());
    }

    @Override
    public Page<DishDto> selectPageByName(Integer page, Integer pageSize, String name) {
        Page<DishDto> dishDtoPage = new Page<>();//目标


        Page<Dish> dishPage = new Page<>(page,pageSize);        //分页查询dish表

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),Dish::getName,name);//模糊查询
        this.page(dishPage,queryWrapper);

        //前端 recodes total
        dishDtoPage.setTotal(dishPage.getTotal());//total

        //records
        ArrayList<DishDto> records = new ArrayList<>();
        dishPage.getRecords().forEach(dish -> {
            //创建DishDto对象
            DishDto dishDto = new DishDto();
            //根据category_id 查询分类名称
            String categoryName = categoryService.getById(dish.getCategoryId()).getName();

            dishDto.setCategoryName(categoryName);

            //属性的拷贝工作
            BeanUtils.copyProperties(dish,dishDto);

            records.add(dishDto);

        });
        dishDtoPage.setRecords(records);
        return dishDtoPage;

    }

    @Override
    public DishDto selectByIdWithFlavors(Long id) {
        DishDto dishDto = new DishDto();//这是最后要返回的数据
        //查询菜品的基本信息
        Dish dish = this.getById(id);
        //查询菜品的口味基本信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        //属性的拷贝
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    @Transactional
    @Override
    public void updateWithFlavors(DishDto dishDto) {
        //更新菜品的基本信息
        updateById(dishDto);
        //更新菜品的口味信息【更新=删除+插入】
        //删除
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //给口味数据设置dishId(这是测试时候出现的bug,这里做个修复)
        dishDto.getFlavors().forEach(dishFlavor -> {
            dishFlavor.setDishId(dishDto.getId());
        });
        //插入
        dishFlavorService.saveBatch(dishDto.getFlavors());
    }

    @Override
    public List<DishDto> listWithFlavors(Dish dish) {
        //查询所有菜品
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        queryWrapper.eq(dish.getStatus()!=null,Dish::getStatus,dish.getStatus());
        List<Dish> dishes = list(queryWrapper);

        //创建DishDto集合
        ArrayList<DishDto> dishDtos = new ArrayList<>();
        dishes.forEach(d ->{
            DishDto dishDto = new DishDto();
            //属性拷贝
            BeanUtils.copyProperties(d,dishDto);
            //查询对应菜品的口味信息
            LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DishFlavor::getDishId,d.getId());
            List<DishFlavor> flavors = dishFlavorService.list(wrapper);
            //把口味信息复制给dto
            dishDto.setFlavors(flavors);

            dishDtos.add(dishDto);
        } );
        return dishDtos;
    }
}
